//
//  FriendsViewController.m
//  NJYouthOlypic
//
//  Created by xiangpaopao on 13-7-20.
//  Copyright (c) 2013年 Xiu. All rights reserved.
//

#import "FriendsViewController.h"



@interface FriendsViewController (){
  UIImageView *logoView;
}

@property (nonatomic, strong) NSMutableArray *friendsArr;
@property (nonatomic, strong)IBOutlet UITableView *listView;
@property (nonatomic, strong)MBProgressHUD *HUD;

@end

@implementation FriendsViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"微博好友"];
    
    //get data
    NSString *path = @"friendships/friends.json";
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:Weibo.weibo.currentAccount.userId forKey:@"uid"];
    [params setObject:@"100" forKey:@"count"];
    
    
    [[WeiboRequest shared] getFromPath:path
                                params:params
                             completed:^(id result, NSData *data, NSError *error) {
                                 if (error) {
                                     NSLog(@"error");
                                 }
                                 else {
                                     [self.HUD hide:YES];
                                     self.friendsArr = [result objectForKey:@"users"];
                                     [self.listView reloadData];
                                 }
                                 
                             }];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:self.HUD];
	self.HUD.delegate = self;
    
    [self.HUD show:YES];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
  return [self.friendsArr count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 40.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"friendCell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
  }
  
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
  cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:1.0];
  
  cell.textLabel.font = [UIFont systemFontOfSize:14];
  cell.textLabel.textColor = [UIColor darkGrayColor];
  cell.textLabel.text = [[self.friendsArr objectAtIndex:indexPath.row] valueForKey:@"name"];
  
  //init cell data
 
  return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  //
  NSString *friendName = [[self.friendsArr objectAtIndex:indexPath.row] valueForKey:@"name"];
  [self.delegate passFriendName:friendName];
   [self.navigationController popViewControllerAnimated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

